package com.cqut.service.impl;

import com.cqut.entity.User;
import com.cqut.mapper.UserMapper;
import com.cqut.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <p>
 * 非空 服务实现类
 * </p>
 *
 * @author 冯永龙
 * @since 2021-06-15
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(User user) {
        if (user.getAccount().equals("")) {
            user.setAccount(null);
        }
        if (user.getMobile().equals("")) {
            user.setMobile(null);
        }
        if (user.getEmail().equals("")) {
            user.setEmail(null);
        }
        return userMapper.login(user);
    }

    @Override
    public boolean register(User user) {
        //先判断该手机号是否被注册
        User useByMobile = userMapper.findUseByMobile(user.getMobile());
        if (useByMobile == null) {
            //根据uuid设置用户的账号
            UUID uuid = UUID.randomUUID();
            user.setAccount(uuid.toString().substring(0, 8));
            return userMapper.register(user) == 1;
        }
        return false;
    }

    @Override
    public User findUserById(int id) {
        return userMapper.findUserById(id);
    }

    @Override
    public User findUserByAccount(String account) {
        return userMapper.findUserByAccount(account);
    }

    @Override
    public boolean updateUserBaseInfo(User user) {
        return userMapper.updateUserBaseInfo(user) == 1;
    }

    @Override
    public boolean updateSettingInfo(User user) {
        return userMapper.updateSettingInfo(user) == 1;
    }

    @Override
    public boolean updateBindingAccount(User user) {
        return userMapper.updateBindingAccount(user) == 1;
    }
}
